package cn.xfdzx.android.apps.shop.activity;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.MyFocusShop_RetailAdapter;
import cn.xfdzx.android.apps.shop.adapter.MyFocusShop_XFDAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.MyFocusShopBean;
import cn.xfdzx.android.apps.shop.bean.StoreInfoBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.MyFocusShopContract;
import cn.xfdzx.android.apps.shop.presenter.MyFocusShopPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.view.SlideRecyclerView;

/*
/* 店铺关注页面
 */
public class ShopFocusActivity extends BaseMvpActivity<MyFocusShopPresenter> implements MyFocusShopContract.View, View.OnClickListener {
    private ImageView ivBack;      //标题栏-左边按钮
    private SmartRefreshLayout mRefreshLayout;
    //private RecyclerView recyclerView;
    private SlideRecyclerView recyclerView;
    private CheckBox checkbox_all;//全选
    private RelativeLayout rl_cancle;//全选布局
    private TextView tv_canclefocus;//取消关注
    private TextView tv_edit;//编辑/完成
    private String limit = "20";
    private int page  = 1;
    private MyFocusShop_RetailAdapter myFocusShopRetailAdapter;//零售关注列表
    private List<MyFocusShopBean.DataBean.RetailBean> retail;
    private MyFocusShop_XFDAdapter myFocusShopXfdAdapter;
    private List<MyFocusShopBean.DataBean.WholesaleBean> wholesale;
    private int pageType;//0-零售1-批发
    private LinearLayout ll_hint;
    private TextView tv_hint;

    @Override
    public int getLayoutId() {
        return R.layout.activity_myshopfocus;
    }

    @Override
    public void initView() {
        pageType = getIntent().getIntExtra("type",0);
        mPresenter = new MyFocusShopPresenter();
        mPresenter.attachView(this);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        recyclerView = findViewById(R.id.recycle_focus);
        ivBack = findViewById(R.id.iv_back);
        rl_cancle = findViewById(R.id.rl_cancle);
        tv_canclefocus = findViewById(R.id.tv_canclefocus);
        tv_edit = findViewById(R.id.tv_edit);
        ll_hint = findViewById(R.id.ll_hint);
        tv_hint = findViewById(R.id.tv_hint);
        ivBack.setOnClickListener(this);
        tv_edit.setOnClickListener(this);
        tv_canclefocus.setOnClickListener(this);
        initRecycle();
        //mPresenter.getFocusList(limit,page);
    }

    private void initRecycle() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        if (pageType == 0){//零售列表
            myFocusShopRetailAdapter = new MyFocusShop_RetailAdapter(this,retail,mPresenter);
            recyclerView.setAdapter(myFocusShopRetailAdapter);
            myFocusShopRetailAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    recyclerView.setOpen();
                }
            });
        }else {
            myFocusShopXfdAdapter = new MyFocusShop_XFDAdapter(this,wholesale,mPresenter);
            recyclerView.setAdapter(myFocusShopXfdAdapter);
            myFocusShopXfdAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    recyclerView.setOpen();
                }
            });
        }

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                page = 1;
                mPresenter.getFocusList(true,true,limit,page);

            }
        });


     }

    @Override
    protected void onResume() {
        super.onResume();
        page = 1;
        mPresenter.getFocusList(true,true,limit,page);
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void getFocusListSuccess(MyFocusShopBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.getFocusList(false,true,limit,page);
                    }
                });
            }

            if (pageType == 0) {//零售列表
                if (page == 1){
                    retail = bean.getData().getRetail();

                    if (EmptyUtil.isEmpty(retail)){
                        ll_hint.setVisibility(View.VISIBLE);
                        tv_hint.setText("您还没有关注零售店铺哦~");
                    }else {
                        ll_hint.setVisibility(View.GONE);
                    }
                }else {
                    retail.addAll(bean.getData().getRetail());
                }
                myFocusShopRetailAdapter.setDate(retail);

                if (EmptyUtil.isEmpty(bean.getData().getRetail())) {
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    page++;
                }
            }else {
                if (page == 1){
                    wholesale = bean.getData().getWholesale();
                    if (EmptyUtil.isEmpty(wholesale)){
                        ll_hint.setVisibility(View.VISIBLE);
                        tv_hint.setText("您还没有关注批发店铺哦~");
                    }else {
                        ll_hint.setVisibility(View.GONE);
                    }
                }else {
                    wholesale.addAll(bean.getData().getWholesale());
                }

                myFocusShopXfdAdapter.setDate(wholesale);
                if (EmptyUtil.isEmpty(bean.getData().getWholesale())) {
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    page++;
                }
            }

        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getCancleFocusSuccess(StoreInfoBeen bean) {
        recyclerView.closeMenu();
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            page = 1;
            mPresenter.getFocusList(true,true,limit,page);
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
//        if (dialog_num.isShowing()) {
//            dialog_num.dismiss();
//
//        }
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_edit:    //编辑/完成

                break;
            case R.id.tv_canclefocus:    //取消关注

                break;


        }
    }
}
